#include "stdafx.h"
#include "fuPageBook.h"

fuPageBook::fuPageBook(wxWindow *parent,wxWindowID id)
: wxChoicebook(parent,id)
{
}

fuPageBook::fuPageBook(wxWindow *parent, wxWindowID id, const wxPoint& pos,const wxSize& size, long style, const wxString& name)
: wxChoicebook(parent,id,pos,size,style,name)
{
	GetChoiceCtrl()->Show(false);
	SetInternalBorder(0);
}

fuPageBook::~fuPageBook(void)
{
}

bool fuPageBook::ShowPage( wxWindow *page )
{
    for(int idx = GetPageCount()-1; idx >= 0; idx--){
        if( GetPage(idx) == page ){
            SetSelection(idx);
            return true;
        }
    }
    return false;
}

wxString fuPageBook::GetPageText( wxWindow *page )
{
    for(int idx = GetPageCount()-1; idx >= 0; idx--){
        if( GetPage(idx) == page ){
            return wxChoicebook::GetPageText(idx);
        }
    }
    return wxEmptyString;
}

//////////////////////////////////////////////////////////////////////////
fuNoteBook::fuNoteBook(wxWindow* parent, 
                       wxWindowID id /*= wxID_ANY*/, 
                       const wxPoint& pos /*= wxDefaultPosition*/,
                       const wxSize& size /*= wxDefaultSize*/, 
                       long style /*= wxAUI_NB_DEFAULT_STYLE*/ ) : 
wxAuiNotebook(parent,id,pos,size,style)
{

}

bool fuNoteBook::AddPage(wxWindow* page,
                         const wxString& caption,
                         bool select /*= false*/,
                         const wxBitmap& bitmap /*= wxNullBitmap*/ )
{
    bool ret = wxAuiNotebook::AddPage(page,caption,select,bitmap);

    PageInfo_t info;
    info.Window = page;
    info.Title = caption;
    //info.Index = GetPageIndex(page);
    info.Image = bitmap;

    m_window[info.Window] = info;
    //m_index [info.Index]  = info;
    m_title [info.Title]  = info;
	return ret;
}

void fuNoteBook::ShowPage( wxWindow * page, bool clear_other )
{
    ShowPage(page,FLAG_SELECT | (clear_other ? FLAG_CLEAR_OTHERS : 0));
}

void fuNoteBook::ShowPage( wxWindow * page,flag_t flag )
{
    if( flag & FLAG_CLEAR_OTHERS ){
        for( int idx = GetPageCount()-1; idx >= 0; idx-- ){
            if( GetPage(idx) != page ){
                GetPage(idx)->Show(false);
                RemovePage(idx);
            }
        }
    }

    if( GetPageIndex(page) >= 0 ){
        SetSelection(GetPageIndex(page));
        return;
    }

    bool select = (flag & FLAG_SELECT) ? true : false;
    map_window_t::iterator iter = m_window.find(page);
    if( iter != m_window.end() ){
        if( iter->second.Image.IsOk() )
            wxAuiNotebook::AddPage(page,iter->second.Title,select,iter->second.Image);
        else
            wxAuiNotebook::AddPage(page,iter->second.Title,select);
    }
}

void fuNoteBook::HidePage( wxWindow * page )
{
    int idx = GetPageIndex(page);
    if( idx >= 0 ){
        GetPage(idx)->Show(false);
        RemovePage(idx);
    }
}

void fuNoteBook::ClearPages( void )
{
	for( int idx = GetPageCount()-1; idx >= 0; idx-- ){
		GetPage(idx)->Show(false);
		RemovePage(idx);
	}
}

wxWindow * fuNoteBook::FindPage( const wxString & text )
{
    map_title_t::iterator iter = m_title.find(text);
    if( iter != m_title.end() )
        return iter->second.Window;
    return NULL;
}

wxWindow * fuNoteBook::GetCurrentPage(void)
{
    return GetPage(GetSelection());
}

//int fuNoteBook::GetCurrentPageIndex( void )
//{
//    if( GetPageCount() ){
//        map_window_t::iterator iter = m_window.find(GetPage(GetSelection()));
//        if( iter != m_window.end() ){
//            return iter->second.Index;
//        }
//    }
//    return -1;
//}
